﻿Imports System.Web

Namespace DotNetNuke.Modules.ActiveForums
#Region "RewardInfo"
    Public Class RewardInfo
        Private _RankId As Integer
        Private _PortalId As Integer
        Private _ModuleId As Integer
        Private _RankName As String
        Private _MinPosts As Integer
        Private _MaxPosts As Integer
        Private _Display As String
        Public Property RankId() As Integer
            Get
                Return _RankId
            End Get
            Set(ByVal Value As Integer)
                _RankId = Value
            End Set
        End Property
        Public Property PortalId() As Integer
            Get
                Return _PortalId
            End Get
            Set(ByVal Value As Integer)
                _PortalId = Value
            End Set
        End Property
        Public Property ModuleId() As Integer
            Get
                Return _ModuleId
            End Get
            Set(ByVal Value As Integer)
                _ModuleId = Value
            End Set
        End Property
        Public Property RankName() As String
            Get
                Return _RankName
            End Get
            Set(ByVal Value As String)
                _RankName = Value
            End Set
        End Property
        Public Property MinPosts() As Integer
            Get
                Return _MinPosts
            End Get
            Set(ByVal Value As Integer)
                _MinPosts = Value
            End Set
        End Property
        Public Property MaxPosts() As Integer
            Get
                Return _MaxPosts
            End Get
            Set(ByVal Value As Integer)
                _MaxPosts = Value
            End Set
        End Property
        Public Property Display() As String
            Get
                Return _Display
            End Get
            Set(ByVal Value As String)
                _Display = Value
            End Set
        End Property

    End Class
#End Region
#Region "RewardController"
    Public Class RewardController
        Public Function Reward_Save(reward As RewardInfo) As RewardInfo
            Dim rankId As Integer = DataProvider.Instance.Ranks_Save(reward.PortalId, reward.ModuleId, reward.RankId, reward.RankName, reward.MinPosts, reward.MaxPosts, reward.Display)
            Return Reward_Get(reward.PortalId, reward.ModuleId, rankId)
        End Function
        Public Sub Reward_Delete(PortalId As Integer, ModuleId As Integer, RankId As Integer)
            DataProvider.Instance.Ranks_Delete(PortalId, ModuleId, RankId)
        End Sub
        Public Function Reward_Get(ByVal PortalId As Integer, ByVal ModuleID As Integer, ByVal RankId As Integer) As RewardInfo
            Dim ri As New RewardInfo
            Dim dr As IDataReader = DataProvider.Instance.Ranks_Get(PortalId, ModuleID, RankId)
            While dr.Read
                With ri
                    .Display = dr("Display").ToString
                    .MaxPosts = CInt(dr("MaxPosts"))
                    .MinPosts = CInt(dr("MinPosts"))
                    .ModuleId = ModuleID
                    .PortalId = PortalId
                    .RankId = CInt(dr("RankId"))
                    .RankName = CStr(dr("RankName"))
                End With
            End While
            dr.Close()
            Return ri
        End Function
        Public Function Reward_List(ByVal PortalId As Integer, ByVal ModuleId As Integer, ByVal UseCache As Boolean) As List(Of RewardInfo)
            Dim cacheKey As String = String.Format(CacheKeys.Rewards, ModuleId.ToString)
            Dim rl As New List(Of RewardInfo)
            If UseCache Then
                rl = CType(DataCache.CacheRetrieve(cacheKey), List(Of RewardInfo))
                If rl Is Nothing Then
                    rl = Reward_List(PortalId, ModuleId)
                    DataCache.CacheStore(cacheKey, rl)
                End If
            Else
                rl = Reward_List(PortalId, ModuleId)
            End If
            Return rl
        End Function
        Public Function Reward_List(ByVal PortalId As Integer, ByVal ModuleId As Integer) As List(Of RewardInfo)
            Dim rl As New List(Of RewardInfo)
            Dim dr As IDataReader = DataProvider.Instance.Ranks_List(PortalId, ModuleId)
            dr.Read()
            dr.NextResult()
            While dr.Read
                Dim ri As New RewardInfo
                With ri
                    .Display = CStr(dr("Display"))
                    .MaxPosts = CInt(dr("MaxPosts"))
                    .MinPosts = CInt(dr("MinPosts"))
                    .ModuleId = ModuleId
                    .PortalId = PortalId
                    .RankId = CInt(dr("RankId"))
                    .RankName = CStr(dr("RankName"))
                End With
                rl.Add(ri)
            End While
            dr.Close()
            Return rl
        End Function
    End Class
#End Region


End Namespace
